//
//  CLMineViewController.swift
//  CLEasyBBQ
//
//  Created by simon zhao on 2017/4/5.
//  Copyright © 2017年 simon zhao. All rights reserved.
//

import UIKit

class CLMineViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    
    let MenuReuStr = "MineMenuCell"
    let InfoReuStr = "MineInfoCell"
    let QuitReuStr = "MineQuitCel"
    let MenuDataArr = ["个人中心","菜单编辑","密码修改"]
    let InfoDataArr = ["店铺名称","马一平","13522318991","高新区昌源北路西城时代A1地块高新区昌源北路西城时"]
    

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "我的"
        
        let CLMineTableview = UITableView.init(frame:UIScreen.main.bounds,  style: .grouped)
        CLMineTableview.delegate = self
        CLMineTableview.dataSource = self
        
        
        
        // Do any additional setup after loading the view.
        let headerView = UIView.init(frame: CGRect(x:0,y:0,width:screenWidth
            ,height:screenHeight * 0.2))
        let QRView = UIImageView.init(image: UIImage.init(named: "QRCode"))
        QRView.frame = CGRect(x:(screenWidth - screenHeight * 0.2 * 0.7)/2,y:screenHeight * 0.2 * 0.15,width:screenHeight * 0.2 * 0.7,height:screenHeight * 0.2 * 0.7)
        headerView.addSubview(QRView)
        CLMineTableview.tableHeaderView = headerView
        //cell自适应行高
        CLMineTableview.estimatedRowHeight = screenHeight * 0.4
        CLMineTableview.rowHeight = UITableViewAutomaticDimension
        
        CLMineTableview.register(UINib.init(nibName: "CLMineMenuSettingCell", bundle: nil), forCellReuseIdentifier: MenuReuStr)
        CLMineTableview.register(UINib.init(nibName: "CLMineInfoTableViewCell", bundle: nil), forCellReuseIdentifier: InfoReuStr)
        CLMineTableview.register(UINib.init(nibName: "CLMineQuitCell", bundle: nil), forCellReuseIdentifier: QuitReuStr)
        self.view.addSubview(CLMineTableview)
        
        
        
    }
    
    


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension CLMineViewController {
    
    @objc(numberOfSectionsInTableView:) func numberOfSections(in tableView: UITableView) -> Int {
        
        return 3
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if section == 0 {
            
            return 1
        }else if section == 1{
            
            return 3
        }else{
            
            return 1
        }
    
    
    
    }
    
    @objc(tableView:heightForRowAtIndexPath:) func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        if indexPath.section == 0 {
            
            return screenHeight * 0.23
        }else if indexPath.section == 1 {
            
            return screenHeight * 0.06
        }else{
            
            return screenHeight * 0.06
        }
    }
    
    @objc(tableView:cellForRowAtIndexPath:) func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 0 {
            
            let cell : CLMineInfoTableViewCell = tableView.dequeueReusableCell(withIdentifier: InfoReuStr, for: indexPath) as! CLMineInfoTableViewCell
            cell.shopNameLabel.text = InfoDataArr[0]
            cell.shopOwnerLabel.text = InfoDataArr[1]
            cell.contactNumLabel.text = InfoDataArr[2]
            cell.shopAddrLabel.text = InfoDataArr[3]
            
            return cell
        }else if indexPath.section == 1 {
            
            let cell : CLMineMenuSettingCell = tableView.dequeueReusableCell(withIdentifier: MenuReuStr, for: indexPath) as! CLMineMenuSettingCell
            cell.settingNameLabel.text = MenuDataArr[indexPath.row]
            return cell
            
        }else{
            
            let cell : CLMineQuitCell = tableView.dequeueReusableCell(withIdentifier: QuitReuStr, for: indexPath) as! CLMineQuitCell
            return cell
        }
        
        
    }
    
    @objc(tableView:didSelectRowAtIndexPath:) func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if indexPath.section == 1 {
            
            if indexPath.row == 0 {
                self.navigationController?.pushViewController(CLPersonInfoViewController(), animated: true)
                
            }else if indexPath.row == 1 {
                self.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(CLEditMenuViewController.init(titleName: "菜单编辑", leftimageName: "itemBack", rightImageName: "Menu_Add"), animated: true)
                self.hidesBottomBarWhenPushed = false
            }else{
                self.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(CLMineModifyPwdViewController.init(titleName: "修改密码", leftimageName: "itemBack", rightImageName: " "), animated: true)
                self.hidesBottomBarWhenPushed = false
                
            }
            
        }
        
        
    }
    
    
}
